package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.configure.FtpUploadConfig;
import com.enjoyor.soa.traffic.util.bean.base.BaseResult;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.FtpHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Auth
@RestController
@RequestMapping({ "/ftp/upload" })
@Api("上传文件到ftp Controller")
@CrossOrigin
public class FtpUploadController {

    @Autowired
    FtpUploadConfig ftpUploadConfig;

    @SuppressWarnings("unused")
    @PostMapping(value = { "/file" }, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传文件到ftp", notes = "上传文件到ftp")
    public String ftpUploadFile(@ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile,
            HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String result = "";
        String ftp_path = ftpUploadConfig.getPath();
        String ftp_ip = ftpUploadConfig.getIp();
        String ftp_port = ftpUploadConfig.getPort();
        String ftp_user = ftpUploadConfig.getUser();
        String ftp_password = ftpUploadConfig.getPassword();
        if (ftp_path == null || ftp_path.equals("")) {
            ftp_path = "tpcmsUpload/0";
        } else {
            ftp_path = ftp_path + "/" + TimeHelper.dateToString(new Date(), "yyyyMMdd");
        }
        String resultUrl = "";
        try {
            InputStream inputStream = multipartFile.getInputStream();
            long fileSize = multipartFile.getSize();
            String fileName = multipartFile.getOriginalFilename();

            String[] fileNames = fileName.split("\\.");
            String filePostfix = fileNames[fileNames.length - 1];
            String fileNewName = TimeHelper.dateToString(new Date(), "yyyyMMddHHmmssSS") + "." + filePostfix;
            boolean flag = FtpHelper.uploadFile(ftp_ip, Integer.valueOf(ftp_port), ftp_user, ftp_password, ftp_path,
                    fileNewName, inputStream);
            if (flag) {
                resultUrl = "ftp://" + ftp_user + ":" + ftp_password + "@" + ftp_ip + ":" + ftp_port + "//" + ftp_path
                        + "//" + fileNewName;
                result = ResultHelper.getResultJson(resultUrl).toString();
            } else {
                result = ResultHelper.getResultJson(EnumAppCode.ERROR_FILE_UPLOAD).toString();
            }
        } catch (Exception e) {
            result = ResultHelper.getResultJson(e, EnumAppCode.OPERATOR_FAILURE, BaseResult.Status.ERROR, "上传异常")
                    .toString();
        }
        return result;
    }
}
